package com.peanut.baby.mvp.ask;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;

import androidx.annotation.NonNull;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.QA;
import com.peanut.baby.model.UploadConf;
import com.peanut.baby.uploader.UploadMgr;
import com.peanut.devlibrary.compressor.Compressor;
import com.tencent.mars.xlog.Log;
import com.yanzhenjie.alertdialog.AlertDialog;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.io.File;
import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;

/**
 * Created by anonymouslystudio on 2017/7/20.
 */

    public class AskPresenter implements AskContract.Presenter{
    AskContract.View view;
    Activity         activity;
    public AskPresenter(AskContract.View view,Activity activity){
        this.view = view;
        this.activity = activity;
    }
    @Override
    public void checkPermissionBeforeSelectPhoto(final Activity activity) {
        AndPermission.with(activity)
                .requestCode(100)
                .permission(Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .rationale(new RationaleListener(){

                    @Override
                    public void showRequestPermissionRationale(int requestCode, final Rationale rationale) {
                        AlertDialog.newBuilder(activity)
                                .setTitle("提醒")
                                .setMessage("你已拒绝过相机或写文件权限，再次拒绝将无法上传照片哦！")
                                .setPositiveButton("授予", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        rationale.resume();
                                    }
                                })
                                .setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        rationale.cancel();
                                    }
                                })
                                .show();
                    }
                })
                .callback(new PermissionListener() {
                    @Override
                    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                        Log.d("LauncherActivity","PermissionListener.SUCCESS");
                        view.onPermissionGranted(grantPermissions);
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                        Log.d("LauncherActivity","PermissionListener.FAIL");
                        view.onPermissionDenied(deniedPermissions);
                    }
                })
                .start();
    }

    @Override
    public void submitQuestion(String type, String title, String desc, String picturePath) {
        Observable observable = RetrofitClient.getInstance().submitQuestion(title,type,InitManager.getInstance().getUserId(),desc,
                picturePath,InitManager.getInstance().getUser().userToken);
        observable.compose(BaseScheduler.<BaseResponse<QA>>compose())
                .compose(RxLifecycle.bind(activity).withObservable()).subscribe(new BaseObserver<QA>() {
            @Override
            protected void onHandleSuccess(QA ad) {
                view.onSubmitQuestionSuccess();
            }

            @Override
            protected void onHandleError(String code,String error) {
                view.onRequestFailed(code,"提问失败 "+error);
            }
        });
    }

    @Override
    public void compressAndUploadPicture(final String path) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                File compressed = Compressor.compress(new File(path));
                UploadMgr.getInstance().uploadImage(compressed.getAbsolutePath(),
                        UploadMgr.UPLOAD_TYPE_LIVE, InitManager.getInstance().getUserId(), new UploadMgr.UploadListener() {
                            @Override
                            public void onQueryToken() {
                            }

                            @Override
                            public void onStartUpload() {

                            }

                            @Override
                            public void onUploadProgress(int total) {

                            }

                            @Override
                            public void onUploadSuccess(UploadConf config) {
                                view.onPictureUploadFinished(true,"",config);
                            }

                            @Override
                            public void onUploadFailed(UploadConf config,String failed) {
                                view.onPictureUploadFinished(false,failed,config);
                            }
                        });
            }
        }).start();
    }
}
